// 车险表单验证方法
import Vue from 'vue'

// 验证插件
import schema from 'async-validator'

// 验证提示模块（带交互）
import noticeModal from '@/components/ivew_components/notice_modal/index.vue'

// 基础验证列表
import carValidateList from'@/components/ivew_conf/car_order_validate_list'

const vueObj:any = new Vue()

/*
**  车险表单验证
**  @params
        orderData: 订单数据
    @back 
        pass:boolean 是否通过
*/
function carValidator(orderData){

    let validator:any = new schema(carValidateList);
    let pass = true

    // 数据验证 
    validator.validate(orderData, (errors, fields) =>{

        if(errors){
            let errInfo = ''
            pass = false
            errors.forEach(err =>{
                errInfo += (err.message + '\n')
            })
            buildErrorNotice(errInfo)
        }
    })   


    return pass

}

// 错误提示

function buildErrorNotice(error:string, title='请补全下列订单信息'){

    vueObj.$Notice.warning({
        duration:0,
        title,
        name: 'carOrderValidator',
        render (h){
            return h(noticeModal, {
                props:{
                    pag: 'carOrderValidator',
                    txt: error,
                }
            })
        }
    })                                    
    
}


export {
    carValidator,
    buildErrorNotice
} 